package com.dyy.proxy.cglib;

import java.lang.reflect.Method;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

/**
 * @author dingyy@jccfc.com
 * @since 2021/12/12 22:19
 */
public class AdminServiceCglibProxy implements MethodInterceptor {

    private final Object target;

    public AdminServiceCglibProxy(Object target) {
        this.target = target;
    }

    /**
     * 获取代理类
     * @return
     */
    public Object getInstance(){
        //工具类
        Enhancer enhancer = new Enhancer();
        //设置父类
        enhancer.setSuperclass(target.getClass());
        //设置回调函数
        enhancer.setCallback(this);
        //创建子类代理对象
        return enhancer.create();
    }

    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        System.out.println("cglib:判断客户是否有权限操作数据变更");
        Object invoke = method.invoke(target);
        System.out.println("cglib:记录客户的LOG日志 ");
        return invoke;
    }
}
